<template>
  <div class="container">
    <div class="top">
      <el-select
        style="margin-bottom: 5px"
        v-model="search.house_code"
        :multiple="false"
        filterable
        remote
        center
        reserve-keyword
        placeholder="请选择仓库"
        :remote-method="entrepolistsMethod"
        :loading="entrepot.loading"
      >
        <el-option
          v-for="item in entrepot.option"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-input
        style="width: 300px"
        placeholder="请输入盘点单编码"
        v-model="search.code"
        clearable
      >
      </el-input>
      <el-date-picker
        value-format="yyyy-MM-dd"
        v-model="date"
        class="hidden-xs-only"
        type="daterange"
        :clearable="false"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-button type="primary" icon="el-icon-search" @click="handleSearch()"
        >查询</el-button
      >
      <el-button type="danger" icon="el-icon-delete" @click="empty()"
        >清空</el-button
      >
      <el-button type="info" icon="el-icon-tickets" @click="derive()"
        >采集明细导出
      </el-button>
      <el-button type="warning" icon="el-icon-tickets" @click="differenceDerive(1)"
        >采集差异导出
      </el-button>
      <el-button
        type="info"
        icon="el-icon-edit-outline"
        @click="differenceDerive(2)"
        >生成差异单</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%;margin-top: 5px;"
      @selection-change="handleSelectionChange"
      empty-text="暂无数据"
    >
      <el-table-column
        :selectable="selectable"
        type="selection"
        width="50"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="serial_code"
        label="单据编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="status_text"
        label="状态"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="dccode"
        label="配送中心编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="dcname"
        label="配送中心名称"
        align="center"
      ></el-table-column>

      <el-table-column
        prop="house_code"
        label="仓库编码"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="house_name"
        label="仓库名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="pd_name"
        label="盘点人"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="remark"
        label="备注"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="particulars(scope)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :title="dialog.title"
      :visible.sync="dialog.dialogVisible"
      width="500px"
      center
    >
      <div class="center" style="margin: 0 auto;width: 300px;">
        <el-select
          v-model="dialog.type"
          style="width: 300px;margin-bottom: 10px;"
          @change="chageType"
          placeholder="请选择类型"
        >
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-select
          v-if="showClass"
          v-model="dialog.class"
          multiple
          style="width: 300px;margin-bottom: 10px;"
          @change="chageClass"
          placeholder="请选择分类"
        >
          <el-option
            v-for="item in classList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-input
          v-if="showCommodity"
          style="width: 300px; margin: 0 auto;margin-bottom: 10px"
          :disabled="true"
          placeholder="请搜索商品信息"
          v-model="commodityNameList.name"
        >
          <template slot="prepend"
            >商品</template
          >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="commodityBtn"
          ></el-button>
        </el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="hideDialog">取 消</el-button>
        <el-button type="primary" :disabled="disabled" @click="confirm"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <commodityDialog
      :commoditydialog="commoditydialog"
      @tag="commodityTag"
    ></commodityDialog>
    <superCollectDetail :dialog="detailDialog"></superCollectDetail>
  </div>
</template>
<script>
import { getToken } from "@/utils/auth";
import SuperCollectDetail from "@/components/check/superCollectDetail";
import CommodityDialog from "@/components/superGrossMargin/commodityDialog";
import Pagination from "@/components/pagination";
import { planList, generateDiff } from "@/api/check/superCheck";
import { selectChooseWarehouse } from "@/api/warehouse/warehouse";
export default {
  components: {
    pagination: Pagination,
    superCollectDetail: SuperCollectDetail,
    commodityDialog: CommodityDialog
  },
  data() {
    return {
      //页面数据
      tableData: [],
      multipleSelection: [],
      search: {
        code: "",
        created_date: "",
        end_date: "",
        house_code: ""
      },
      disabled: true,
      showClass: false,
      showCommodity: false,
      commodityNameList: {
        name: "",
        code: ""
      },
      commoditydialog: {
        centerDialogVisible: false
      },
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      detailDialog: {
        dialogVisible: false,
        id: ""
      },
      dialog: {
        dialogVisible: false,
        title: "",
        status: "",
        type: "",
        class: []
      },

      typeList: [
        {
          value: 0,
          label: "全场"
        },
        {
          value: 1,
          label: "分类"
        },
        {
          value: 2,
          label: "商品"
        }
      ],
      classList: [
        {
          value: "粮油",
          label: "粮油"
        },
        {
          value: "超市",
          label: "超市"
        },
        {
          value: "休闲食品",
          label: "休闲食品"
        },
        {
          value: "干果",
          label: "干果"
        },
        {
          value: "炒货",
          label: "炒货"
        },
        {
          value: "包装物",
          label: "包装物"
        },
        {
          value: "面食",
          label: "面食"
        }
      ],
      entrepot: {
        innerVisible: false,
        option: [],
        list: [],
        loading: false
      },
      date: []
    };
  },
  created() {
    // this.getData();
    this.getoptionData();
  },
  methods: {
    //获取页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        serial_code: this.search.code,
        start_time: this.search.created_date,
        end_time: this.search.end_date,
        house_code: this.search.house_code
      };
      planList(data)
        .then(res => {
          if (res.code == 0) {
            this.tableData = res.data.paginate.data;
            this.pageData.page = res.data.paginate.current_page;
            this.pageData.pagesize = res.data.paginate.per_page;
            this.pageData.total = res.data.paginate.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 获取仓库方法
    entrepolistsMethod(query) {
      if (query !== "") {
        this.getoptionData(query);
      } else {
        this.entrepot.options = [];
      }
    },
    getoptionData(name) {
      let data = {
        page: 1,
        limit: 500,
        name: name
      };
      //获取仓库
      selectChooseWarehouse(data).then(res => {
        if (res.data.data) {
          this.entrepot.option = res.data.data.map(item => {
            return { value: `${item.house_code}`, label: `${item.house_name}` };
          });
        }
      });
    },

    //导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          if (this.date.length > 0) {
            this.search.created_date = this.date[0];
            this.search.end_date = this.date[1];
          }
          let link = document.createElement("a");
          link.href =
            this.$axios.defaults.baseURL +
            "/smkapi/Stocktaking/exportPlanList?token=" +
            getToken() +
            "&serial_code=" +
            this.search.code +
            "&house_code=" +
            this.search.house_code +
            "&start_time=" +
            this.search.created_date +
            "&end_time=" +
            this.search.end_date;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出"
          });
        });
    },
    // 商超手持盘点采集差异导出
    differenceDerive(status) {
      console.log(this.multipleSelection, 89898);
      if (this.multipleSelection.length > 0) {
        this.dialog.dialogVisible = true;
        this.dialog.title =
          status == 1
            ? "商超手持盘点采集差异导出"
            : "生成商超手持盘点采集差异单";
        this.dialog.status = status;
      } else {
        this.$message({
          type: "info",
          message: "请勾选内容"
        });
      }
    },
    chageType() {
      console.log(this.dialog.type);
      switch (this.dialog.type) {
        case 0:
          this.disabled = false;
          this.dialog.class = [];
          this.showClass = false;
          this.showCommodity = false;
          this.commodityNameList = {
            name: "",
            code: ""
          };
          break;
        case 1:
          this.disabled = true;
          this.showClass = true;
          this.showCommodity = false;
          this.commodityNameList = {
            name: "",
            code: ""
          };
          break;
        case 2:
          this.dialog.class = [];
          this.disabled = true;
          this.showClass = false;
          this.showCommodity = true;
          break;
      }
    },
    chageClass() {
      this.disabled = false;
    },
    //超级商品搜索
    commodityBtn() {
      this.commoditydialog = {
        centerDialogVisible: true
      };
    },
    commodityTag(data) {
      if (data.length > 0) {
        this.commodityNameList = {
          name: "",
          code: ""
        };
        let arrName = [];
        let arrCode = [];
        data.forEach(res => {
          arrName.push(res.name);
          arrCode.push(res.code);
        });
        this.commodityNameList.name = arrName.join(",");
        this.commodityNameList.code = arrCode.join(",");
        this.disabled = false;
      }
    },
    //关闭
    hideDialog() {
      this.dialog = {
        dialogVisible: false,
        title: "",
        status: "",
        type: "",
        class: []
      };
      this.disabled = true;
      this.showClass = false;
      this.showCommodity = false;
      this.commodityNameList = {
        name: "",
        code: ""
      };
    },
    //提交
    confirm() {
      let ids = [];
      this.multipleSelection.forEach(res => {
        ids.push(res.id);
      });
      if (this.dialog.status == 1) {
        let link = document.createElement("a");
        link.href =
          this.$axios.defaults.baseURL +
          "/smkapi/Stocktaking/exportPlanDiff?token=" +
          getToken() +
          "&ids=" +
          ids.join(",") +
          "&goodscodes=" +
          this.commodityNameList.code +
          "&type=" +
          this.dialog.type +
          "&class=" +
          this.dialog.class.join(",");
        link.click();
        this.hideDialog();
      } else {
        let data = {
          ids: ids.join(","),
          goodscodes: this.commodityNameList.code,
          type: this.dialog.type,
          class: this.dialog.class.join(",")
        };
        generateDiff(data).then(res => {
          if (res.code == 0) {
            this.$message({
              type: "success",
              message: res.msg || res.message
            });
            this.hideDialog();
            this.getData();
          } else {
            this.$message({
              type: "info",
              message: res.msg || res.message
            });
          }
        });
      }
    },
    //详情
    particulars(scope) {
      this.detailDialog.dialogVisible = true;
      this.detailDialog.id = scope.row.id;
    },
    // 搜索
    handleSearch() {
      if (this.search.house_code) {
        if (this.date.length > 0) {
          this.search.created_date = this.date[0];
          this.search.end_date = this.date[1];
        }
        this.pageData.page = 1;
        this.pageData.total = null;
        this.getData();
      } else {
        this.$message({
          type: "info",
          message: "请选择仓库"
        });
      }
    },
    //清空
    empty() {
      this.search = {
        code: "",
        created_date: "",
        end_date: ""
      };
      this.date = [];
    },

    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    selectable(row, index) {
      if (row.status == "3") {
        return false;
      } else {
        return true;
      }
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
  /* display: flex; */
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>
